/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ytmc.downloader;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.net.MalformedURLException;
import java.util.ArrayList;
import ytmc.FileSize;
import ytmc.TimePeriod;
import ytmc.TransferSpeed;

/**
 *
 * @author K
 */
public class DownloadTask
{
    private String url;
    private String directVideoURL;
    private String savePath;
    private String title;
    private FileSize size;
    private DownloadTaskStatus status;
    private int progress;
    private TransferSpeed downloadSpeed;
    private TimePeriod remainingTime;
    private boolean complete;
    private ArrayList<PropertyChangeListener> listeners;

    private final Object lock = new Object();

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    public String getDirectVideoURL()
    {
        return directVideoURL;
    }

    public void setDirectVideoURL(String directVideoURL)
    {
        this.directVideoURL = directVideoURL;
    }

    public String getSavePath()
    {
        return savePath;
    }

    public void setSavePath(String savePath)
    {
        this.savePath = savePath;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public FileSize getSize()
    {
        return size;
    }

    public void setSize(FileSize size)
    {
        this.size = size;
    }

    public DownloadTaskStatus getStatus()
    {
        return status;
    }

    public void setStatus(DownloadTaskStatus status)
    {
        this.status = status;
    }

    public int getProgress()
    {
        return progress;
    }

    public void setProgress(int progress)
    {
        this.progress = progress;
    }

    public TransferSpeed getDownloadSpeed()
    {
        return downloadSpeed;
    }

    public void setDownloadSpeed(TransferSpeed downloadSpeed)
    {
        this.downloadSpeed = downloadSpeed;
    }

    public TimePeriod getRemainingTime()
    {
        return remainingTime;
    }

    public void setRemainingTime(TimePeriod remainingTime)
    {
        this.remainingTime = remainingTime;
    }

    public boolean isComplete()
    {
        synchronized (lock)
        {
            return complete;
        }
    }

    public void setComplete(boolean complete)
    {
        synchronized (lock)
        {
            boolean oldValue = complete;
            this.complete = complete;
            notifyPropertyChangeListeners(new PropertyChangeEvent(this, "complete", oldValue, complete));
        }
    }

    public DownloadTask(String url, String savePath) throws MalformedURLException
    {
        this.listeners = new ArrayList<PropertyChangeListener>();
        this.url = url;
        this.savePath = savePath;
        this.directVideoURL = "";
        this.title = "";
        this.size = new FileSize(0);
        this.status = DownloadTaskStatus.NOT_STARTED;
        this.downloadSpeed = new TransferSpeed(0);
        this.remainingTime = new TimePeriod(0);
        this.complete = false;

        validate();
    }

    private void validate() throws MalformedURLException
    {
        if(!url.startsWith("http://www.youtube.com/watch"))
        {
            throw new MalformedURLException("Podany link nie jest prawidłowym linkiem do wideo serwisu YouTube.");
        }
    }

    public void addPropertyChangeListener(PropertyChangeListener listener)
    {
        synchronized (lock)
        {
            if (!listeners.contains(listener))
            {
                listeners.add(listener);
            }
        }
    }

    public void removePropertyChangeListener(PropertyChangeListener listener)
    {
        synchronized (lock)
        {
            if (listeners.contains(listener))
            {
                listeners.remove(listener);
            }
        }
    }

    private void notifyPropertyChangeListeners(PropertyChangeEvent event)
    {
        for (PropertyChangeListener propertyChangeListener : listeners)
        {
            propertyChangeListener.propertyChange(event);
        }
    }
}
